# Contributor: Oleg A. Khlybov <fougas@mail.ru>

_realname=scotch
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-int64")
pkgdesc='Graph partitioning and sparse matrix ordering package (mingw-w64)'
pkgver=7.0.4
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-msmpi")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
options=('!strip' 'staticlibs')
license=('spdx:CECILL-C')
url="https://www.labri.fr/perso/pelegrin/scotch/"
source=("https://gitlab.inria.fr/scotch/scotch/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.gz"
        "Makefile.idx32.inc"
        "Makefile.idx64.inc"
        "0002-pipe-fix.patch"
        "0004-dummysizes-regex.patch")
sha256sums=('8ef4719d6a3356e9c4ca7fefd7e2ac40deb69779a5c116f44da75d13b3d2c2c3'
            '4221b69dcc53230ff4d6bc18e3982f6d39971a99fc17f4dab609fc19b99d8a30'
            'c52bf0598332d8139a37606e7ea9376b7f841059047c0c977b205a763fc7e97a'
            'b6e76b1d2f35b3fcc771b97a35a468d6314ee3a619defef18b82fc7654638a49'
            '15dd82d4c7b118e08316df4242d647bdaa1f180ff143ee33fe84633c97aac111')

prepare() {
  cd "${srcdir}/${_realname}-v${pkgver}"
  for p in ${source[*]:3}; do
    patch -p1 -i "$srcdir/$p"
  done
}

_build_scotch() {
  _idx_makefile_inc=$1
  _idx_suffix=$2

  # remove previous build artifacts
  cd "${srcdir}/${_realname}-v${pkgver}"
  rm -rfd include lib bin include${_idx_suffix} lib${_idx_suffix} bin${_idx_suffix}

  cp -p "${srcdir}/${_idx_makefile_inc}" "${srcdir}/${_realname}-v${pkgver}/src/Makefile.inc"
  cd "${srcdir}/${_realname}-v${pkgver}/src"
  make scotch ptscotch esmumps ptesmumps

  cd ../lib
  ${CC} -shared -o lib${_realname}.dll -Wl,--enable-auto-import -Wl,--export-all-symbols,-no-undefined \
    -Wl,--out-implib,lib${_realname}.dll.a -Wl,--whole-archive lib${_realname}.a lib${_realname}err.a -Wl,--no-whole-archive \
    -pthread
  mpicc -shared -o libpt${_realname}.dll -Wl,--enable-auto-import -Wl,--export-all-symbols,-no-undefined \
    -Wl,--out-implib,libpt${_realname}.dll.a -Wl,--whole-archive libpt${_realname}.a libpt${_realname}err.a -Wl,--no-whole-archive \
    -L. -l${_realname} -pthread

  cd "${srcdir}/${_realname}-v${pkgver}"
  mv include include${_idx_suffix}
  mv lib lib${_idx_suffix}
  mv bin bin${_idx_suffix}
  cd include${_idx_suffix}
  mv metis.h scotchmetis.h
  mv parmetis.h ptscotchparmetis.h
}

build()
{
  msg2 "Build Scotch with 32-bit indexing"
  _build_scotch "Makefile.idx32.inc" "32"

  msg2 "Build Scotch with 64-bit indexing"
  _build_scotch "Makefile.idx64.inc" "64"
}


_package_scotch() {
  _idx_suffix=$1

  cd "${srcdir}/${_realname}-v${pkgver}"
  mkdir -p ${pkgdir}${MINGW_PREFIX}/{bin,lib,include}
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  echo "
	prefix=${MINGW_PREFIX}
	libdir=\${prefix}/lib
	includedir=\${prefix}/include
	Name: scotch
	URL: ${url}
	Version: ${pkgver}
	Description: Serial graph partitioning and sparse matrix ordering package
	Cflags: -I\${includedir}
	Libs.private: -lscotcherr
	Libs: -L\${libdir} -lscotch
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/scotch.pc"
  echo "
	prefix=${MINGW_PREFIX}
	libdir=\${prefix}/lib
	includedir=\${prefix}/include
	Name: ptscotch
	URL: ${url}
	Version: ${pkgver}
	Description: Parallel graph partitioning and sparse matrix ordering package
	Requires: scotch
	Requires.private: msmpi
	Cflags: -I\${includedir}
	Libs.private: -lptscotcherr
	Libs: -L\${libdir} -lptscotch
  " | sed '/^\s*$/d;s/^\s*//' > "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/ptscotch.pc"
  (
    cd include${_idx_suffix}
    install -m644 *.h "${pkgdir}${MINGW_PREFIX}/include"
  )
  (
    cd lib${_idx_suffix}
    install -m644 lib*.a "${pkgdir}${MINGW_PREFIX}/lib"
    install -m644 lib*.dll "${pkgdir}${MINGW_PREFIX}/bin"
  )
}

package_scotch()
{
  _package_scotch "32"
}

package_scotch-int64() {
  pkgdesc="Graph partitioning and sparse matrix ordering package with 64-bit indexing (mingw-w64)"
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  _package_scotch "64"
}


# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
